package com.ruoyi.project.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.IdUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.domain.InsProjectReagentMonth;
import com.ruoyi.project.product.domain.InsProjectReagentMonthDTO;
import com.ruoyi.project.product.domain.InsProjectReagentMonthDetail;
import com.ruoyi.project.product.service.IInsProjectReagentMonthDetailService;
import com.ruoyi.project.product.service.IInsProjectReagentMonthService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 产品效期月代码Controller
 *
 * @author taney
 * @date 2020-12-15
 */
@AllArgsConstructor
@RestController
@RequestMapping("/product/projectReagentMonth")
public class InsProjectReagentMonthController extends BaseController {

    private final IInsProjectReagentMonthService iInsProjectReagentMonthService;
    private final IInsProjectReagentMonthDetailService iInsProjectReagentMonthDetailService;

    /**
     * 查询产品效期月代码列表
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentMonth:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsProjectReagentMonth insProjectReagentMonth) {
        startPage();
        LambdaQueryWrapper<InsProjectReagentMonth> lqw = new LambdaQueryWrapper<InsProjectReagentMonth>();
        if (StringUtils.isNotBlank(insProjectReagentMonth.getMachineType())) {
            lqw.eq(InsProjectReagentMonth::getMachineType, insProjectReagentMonth.getMachineType());
        }
        if (insProjectReagentMonth.getProjectId() != null) {
            lqw.eq(InsProjectReagentMonth::getProjectId, insProjectReagentMonth.getProjectId());
        }
        if (StringUtils.isNotBlank(insProjectReagentMonth.getReagentType())) {
            lqw.eq(InsProjectReagentMonth::getReagentType, insProjectReagentMonth.getReagentType());
        }
        List<InsProjectReagentMonthDTO> list = iInsProjectReagentMonthService.listInsProjectReagentMonthDTO(lqw);
        list.forEach(projectReagentMonthDTO -> {
            String randomUuid = projectReagentMonthDTO.getRandomUuid();
            LambdaQueryWrapper<InsProjectReagentMonth> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(InsProjectReagentMonth::getRandomUuid, randomUuid);
            List<InsProjectReagentMonth> projectReagentMonthList = iInsProjectReagentMonthService.list(lqw2);
            List<Integer> projectIds = projectReagentMonthList.stream()
                    .map(InsProjectReagentMonth::getProjectId)
                    .collect(Collectors.toList());
            projectReagentMonthDTO.setProjectIds(projectIds);
        });
        return getDataTable(list);
    }

    /**
     * 导出产品效期月代码列表
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentMonth:export')")
    @Log(title = "产品效期月代码", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsProjectReagentMonth insProjectReagentMonth) {
        LambdaQueryWrapper<InsProjectReagentMonth> lqw = new LambdaQueryWrapper<InsProjectReagentMonth>(insProjectReagentMonth);
        List<InsProjectReagentMonth> list = iInsProjectReagentMonthService.list(lqw);
        ExcelUtil<InsProjectReagentMonth> util = new ExcelUtil<InsProjectReagentMonth>(InsProjectReagentMonth.class);
        return util.exportExcel(list, "projectReagentMonth");
    }

    /**
     * 获取产品效期月代码详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentMonth:query')")
    @GetMapping(value = "/{randomUuid}")
    public AjaxResult getInfo(@PathVariable("randomUuid") String randomUuid) {
        LambdaQueryWrapper<InsProjectReagentMonth> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(InsProjectReagentMonth::getRandomUuid, randomUuid);

        LambdaQueryWrapper<InsProjectReagentMonthDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(InsProjectReagentMonthDetail::getRandomUuid, randomUuid);
        List<InsProjectReagentMonthDetail> detailList = iInsProjectReagentMonthDetailService.list(lqw);

        List<InsProjectReagentMonth> projectReagentMonthList = iInsProjectReagentMonthService.list(lqw2);
        List<Integer> projectIds = projectReagentMonthList.stream()
                .map(InsProjectReagentMonth::getProjectId)
                .collect(Collectors.toList());

        InsProjectReagentMonthDTO projectReagentMonthDTO = new InsProjectReagentMonthDTO();
        BeanUtils.copyProperties(projectReagentMonthList.get(0), projectReagentMonthDTO);
        projectReagentMonthDTO.setDetailList(detailList);
        projectReagentMonthDTO.setProjectIds(projectIds);

        return AjaxResult.success(projectReagentMonthDTO);
    }

    /**
     * 新增产品效期月代码
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentMonth:add')")
    @Log(title = "产品效期月代码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsProjectReagentMonthDTO projectReagentMonthDTO) {
        List<Integer> projectIds = projectReagentMonthDTO.getProjectIds();
        List<InsProjectReagentMonthDetail> detailList = projectReagentMonthDTO.getDetailList();
        List<InsProjectReagentMonth> projectReagentMonthList = new ArrayList<>();
        String uuid = IdUtils.fastSimpleUUID();
        for (Integer projectId : projectIds) {
            InsProjectReagentMonth projectReagentMonth = new InsProjectReagentMonth();
            projectReagentMonth.setProjectId(projectId);
            projectReagentMonth.setReagentType(projectReagentMonthDTO.getReagentType());
            projectReagentMonth.setMachineType(projectReagentMonthDTO.getMachineType());
            projectReagentMonth.setRandomUuid(uuid);
            projectReagentMonthList.add(projectReagentMonth);
        }
        detailList.forEach(one -> one.setRandomUuid(uuid));
        boolean saveBatch = iInsProjectReagentMonthService.saveBatch(projectReagentMonthList);
        boolean saveBatch1 = iInsProjectReagentMonthDetailService.saveBatch(detailList);
        return toAjax(saveBatch1 && saveBatch ? 1 : 0);
    }

    /**
     * 修改产品效期月代码
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentMonth:edit')")
    @Log(title = "产品效期月代码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsProjectReagentMonthDTO projectReagentMonthDTO) {
        String randomUuid = projectReagentMonthDTO.getRandomUuid();
        LambdaQueryWrapper<InsProjectReagentMonth> lqw = new LambdaQueryWrapper<>();
        lqw.eq(InsProjectReagentMonth::getRandomUuid, randomUuid);

        LambdaQueryWrapper<InsProjectReagentMonthDetail> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(InsProjectReagentMonthDetail::getRandomUuid, randomUuid);
        iInsProjectReagentMonthService.remove(lqw);
        iInsProjectReagentMonthDetailService.remove(lqw2);

        List<Integer> projectIds = projectReagentMonthDTO.getProjectIds();
        List<InsProjectReagentMonthDetail> detailList = projectReagentMonthDTO.getDetailList();
        List<InsProjectReagentMonth> projectReagentMonthList = new ArrayList<>();
        String uuid = IdUtils.fastSimpleUUID();
        for (Integer projectId : projectIds) {
            InsProjectReagentMonth projectReagentMonth = new InsProjectReagentMonth();
            projectReagentMonth.setProjectId(projectId);
            projectReagentMonth.setReagentType(projectReagentMonthDTO.getReagentType());
            projectReagentMonth.setMachineType(projectReagentMonthDTO.getMachineType());
            projectReagentMonth.setRandomUuid(uuid);
            projectReagentMonthList.add(projectReagentMonth);
        }
        detailList.forEach(one -> one.setRandomUuid(uuid));
        boolean saveBatch = iInsProjectReagentMonthService.saveBatch(projectReagentMonthList);
        boolean saveBatch1 = iInsProjectReagentMonthDetailService.saveBatch(detailList);
        return toAjax(saveBatch && saveBatch1 ? 1 : 0);
    }

    /**
     * 删除产品效期月代码
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentMonth:remove')")
    @Log(title = "产品效期月代码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{randomUuids}")
    public AjaxResult remove(@PathVariable String[] randomUuids) {
        for (String randomUuid : randomUuids) {
            LambdaQueryWrapper<InsProjectReagentMonth> lqw = new LambdaQueryWrapper<>();
            lqw.eq(InsProjectReagentMonth::getRandomUuid, randomUuid);

            LambdaQueryWrapper<InsProjectReagentMonthDetail> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(InsProjectReagentMonthDetail::getRandomUuid, randomUuid);
            iInsProjectReagentMonthService.remove(lqw);
            iInsProjectReagentMonthDetailService.remove(lqw2);
        }

        return AjaxResult.success();
    }

}
